<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<%@page import="deweb.cimaplanning.misc"%>
<%@page import="com.cs.common.pool.DBDAO"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="deweb.cimaplanning.beans.MonthsPeriodsVersionsBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="deweb.cimaplanning.dto.MonthsPeriodsVersions"%>
<%@page import="deweb.cimaplanning.beans.PlantsVersionsMonthsBean"%>
<%@page import="deweb.cimaplanning.dto.PlantsVersionsMonths"%>
<%@page import="deweb.cimaplanning.beans.CostsPlantsBean"%>
<%@page import="deweb.cimaplanning.dto.CostsPlants"%>
<%@page import="java.text.DecimalFormat"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../css/principal.css" type="text/css">
<link rel="stylesheet" href="../css/secundario.css" type="text/css">
<title>Costos de Plantas - Tipo de Maíz</title>
</head>
<body>

<%
boolean DEBUG = false;
if (DEBUG) out.println("<br>DEBUG ON: [" + request.getRequestURI() + "]");

//bloque de contadores y elementos de ciclo
int i;
int j;
int k;

boolean odd;

DBDAO dbConn	= null;
Connection conn = null;
Statement stmt  = null;
ResultSet rs	= null;

String sql 		= null;


try{
	dbConn	= new DBDAO();
    conn	= dbConn.getConnection(misc.DS_NAME); 
    stmt	= conn.createStatement();
    
	sql = "";
	
	String keyId	= request.getParameter("keyId") == null ? "" : request.getParameter("keyId");
	String plant	= request.getParameter("plant") == null ? "" : request.getParameter("plant");
	String waxy		= request.getParameter("waxy") == null ? "" : request.getParameter("waxy");
	String cons		= request.getParameter("cons") == null ? "false" : request.getParameter("cons");
	
	if (DEBUG) {
		//version
		out.println("<br>keyId: [" + keyId + "]");
		//plant_id
		out.println("<br>plant: [" + plant + "]");
		//false los no waxy, true los waxy
		out.println("<br>waxy: [" + waxy + "]");
	} 
	
	//eliminacion de registro (NA)
	String delMsg = "";
	
/*
	//version quemada para facilitar las cosas
	keyId = "1";
	out.println("<br>Version quemada para facilidad de desarrollo: [" + keyId + "]");
	
	//planta quemada para facilitar las cosas
	plant = "3303";
	out.println("<br>Planta quemada para facilidad de desarrollo: [" + plant + "]");

	//waxy quemado para facilitar las cosas
	waxy = "false";
	out.println("<br>Waxy quemado para facilidad de desarrollo: [" + waxy + "]");
*/

	if (keyId.length() == 0 || plant.length() == 0 || waxy.length() == 0){
		out.println("<br>Seleccionar el Reporte requerido");
		return;
	}

	int versionId;
	int plantId;
	boolean isWaxy;

	try {
		versionId	= Integer.parseInt(keyId);
		plantId		= Integer.parseInt(plant);
		isWaxy		= waxy.toLowerCase().equals("true"); //Boolean.getBoolean(waxy);
	} catch (Exception eParse) {
		System.out.println("[cost_per_plant_corn_type]: " + eParse.getMessage());
		return;
	}

	sql  = "SELECT version_id, short_name, description, cost ";
	sql += "	FROM vw_versions_processes_status ";
	sql += "	WHERE version_id = " + versionId + " ";
	
	if (DEBUG) out.println("<br>sql: [" + sql + "]");
	rs = stmt.executeQuery(sql);
	
	int cost = -1;
	String versionShortName = "";
	
	if (rs.next()){
		cost 				= rs.getInt("cost");
		versionShortName	= rs.getString("short_name");
	}
	
	if (cost < 0) {
		out.println("<br>Versión id [" + versionId + "] no existe");
		return;
	} else if (cost == 0){
		out.println("<br>No hay resultados de costos para versión '" + versionShortName + "', favor de 'Calcular Costos'");
		return;
	}
	
	//listado de los meses de la version
	MonthsPeriodsVersionsBean mpvBean	= new MonthsPeriodsVersionsBean();
	ArrayList aMonthPeriodsVersions 	= mpvBean.browse(versionId);
	MonthsPeriodsVersions mpvDto 		= null;
	
	//listado de las plants versions de la version
	PlantsVersionsMonthsBean pvBean		= new PlantsVersionsMonthsBean();
	ArrayList aPlantsVersions			= null;
	PlantsVersionsMonths pvDto			= null;
%>

<form name="mainFrm" method="POST">

<%
	out.println("  <table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");
	out.println("    <tr>");
	out.println("      <td><div class=\"frame_hs\" id=\"theFrame\">");

	sql  = "SELECT corn_type_id, code, description, corn_origin_id ";
	sql += "	FROM corns_types ";
	sql += "	WHERE description " + (isWaxy ? "" : "NOT") + " LIKE '%waxy%' ";
	
	if (DEBUG) out.println("<br>sql: [" + sql + "]");
	rs = stmt.executeQuery(sql);
	
	int cornTypeId;
	
	//listado de los costos por tipo de maíz
	CostsPlantsBean cpBean		= new CostsPlantsBean();
	ArrayList aCostsPlants		= null;
	ArrayList aCostsPlantsFL	= null;		//perdida de futuros
	ArrayList aTotals			= null;
	ArrayList aTotalsFL			= null;
	CostsPlants cpDto			= null;
	CostsPlants cpflDto			= null;		//dto para perdida de futuros
	CostsPlants cpTotalDto		= null;
	
	//formatos
	DecimalFormat cf = new DecimalFormat("#,##0.0000");		//currency format
	DecimalFormat tf = new DecimalFormat("#,##0.000");		//tons format
	
	String value;
	
	int months	= aMonthPeriodsVersions.size();
	aTotals 	= new ArrayList(months);
	aTotalsFL 	= new ArrayList(months);
	
	//llenarlo en ceros
	for (i = 0; i < months; i++){
		aTotals.add(new CostsPlants());
		aTotalsFL.add(new CostsPlants());
	}
	
	
	out.println("        <table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">");
	
	while (rs.next()){
		cornTypeId = rs.getInt("corn_type_id");
		System.out.println("cornTypeId [" + cornTypeId + "]");
		
		aCostsPlants	= cpBean.costsReportsBrowseByCornType(versionId, plantId, cornTypeId);
		aCostsPlantsFL	= cpBean.costsReportsBrowseByCornTypeFutLoss(versionId, plantId, cornTypeId);
		
		//integración de totales
		for (i = 0; i < months; i++){
			//costos
			cpTotalDto	= (CostsPlants)aTotals.get(i);
			cpDto		= (CostsPlants)aCostsPlants.get(i);
			
			cpTotalDto = cpBean.add(cpTotalDto, cpDto);
			
			aTotals.set(i, cpTotalDto);
			
			//futuros
			cpTotalDto	= (CostsPlants)aTotalsFL.get(i);
			cpflDto		= (CostsPlants)aCostsPlantsFL.get(i);
			
			cpTotalDto = cpBean.add(cpTotalDto, cpflDto);
			
			aTotalsFL.set(i, cpTotalDto);
		}
		
		out.println("          <tr>");
		out.println("            <td class=\"cb_titulocolumna cellBorder\">" + rs.getString("code") + " - " + rs.getString("description") + "</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aMonthPeriodsVersions.size(); i++){
			mpvDto = (MonthsPeriodsVersions)aMonthPeriodsVersions.get(i);
			
			out.println("            <td align=\"center\" class=\"cb_titulocolumna cellBorder\">" + mpvDto.getShortName() + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">Tons</td>");
		out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getInitialTons() > 0.0 ? tf.format(cpDto.getInitialTons()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}

		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Entradas</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getIncomingTons() > 0.0 ? tf.format(cpDto.getIncomingTons()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Disponible</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getAvailableTons() > 0.0 ? tf.format(cpDto.getAvailableTons()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoNon\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getGrindTons() > 0.0 ? tf.format(cpDto.getGrindTons()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Saldo Final</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getFinalTons() > 0.0 ? tf.format(cpDto.getFinalTons()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">USD Maíz</td>");
		out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getInitialUsd() > 0.0 ? cf.format(cpDto.getInitialUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}

		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Entradas</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getIncomingUsd() > 0.0 ? cf.format(cpDto.getIncomingUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Disponible</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getAvailableUsd() > 0.0 ? cf.format(cpDto.getAvailableUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoNon\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getGrindUsd() > 0.0 ? cf.format(cpDto.getGrindUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Saldo Final</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto = (CostsPlants)aCostsPlants.get(i);
			
			value = cpDto.getFinalUsd() > 0.0 ? cf.format(cpDto.getFinalUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">USD Futuros</td>");
		out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlantsFL.size(); i++){
			cpflDto = (CostsPlants)aCostsPlantsFL.get(i);
			
			value = Math.abs(cpflDto.getInitialUsd()) > 0.0 ? cf.format(cpflDto.getInitialUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}

		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Entradas</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlantsFL.size(); i++){
			cpflDto = (CostsPlants)aCostsPlantsFL.get(i);
			
			value = Math.abs(cpflDto.getIncomingUsd()) > 0.0 ? cf.format(cpflDto.getIncomingUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Disponible</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlantsFL.size(); i++){
			cpflDto = (CostsPlants)aCostsPlantsFL.get(i);
			
			value = Math.abs(cpflDto.getAvailableUsd()) > 0.0 ? cf.format(cpflDto.getAvailableUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoNon\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlantsFL.size(); i++){
			cpflDto = (CostsPlants)aCostsPlantsFL.get(i);
			
			value = Math.abs(cpflDto.getGrindUsd()) > 0.0 ? cf.format(cpflDto.getGrindUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Saldo Final</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlantsFL.size(); i++){
			cpflDto = (CostsPlants)aCostsPlantsFL.get(i);
			
			value = Math.abs(cpflDto.getFinalUsd()) > 0.0 ? cf.format(cpflDto.getFinalUsd()) : misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">TOTAL USD</td>");
		out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto 	= (CostsPlants)aCostsPlants.get(i);
			cpflDto	= (CostsPlants)aCostsPlantsFL.get(i);
			
			value = (cpDto.getInitialUsd() + cpflDto.getInitialUsd()) > 0.0 
							? cf.format(cpDto.getInitialUsd() + cpflDto.getInitialUsd()) 
							: misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}

		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Entradas</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto 	= (CostsPlants)aCostsPlants.get(i);
			cpflDto	= (CostsPlants)aCostsPlantsFL.get(i);
			
			value = (cpDto.getIncomingUsd() + cpflDto.getIncomingUsd()) > 0.0 
							? cf.format(cpDto.getIncomingUsd() + cpflDto.getIncomingUsd()) 
							: misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Disponible</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto 	= (CostsPlants)aCostsPlants.get(i);
			cpflDto	= (CostsPlants)aCostsPlantsFL.get(i);
			
			value = (cpDto.getAvailableUsd() + cpflDto.getAvailableUsd()) > 0.0 
							? cf.format(cpDto.getAvailableUsd() + cpflDto.getAvailableUsd()) 
							: misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoNon\">");
		out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
		out.println("            <td class=\"cleanCell\">&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto 	= (CostsPlants)aCostsPlants.get(i);
			cpflDto	= (CostsPlants)aCostsPlantsFL.get(i);
			
			value = (cpDto.getGrindUsd() + cpflDto.getGrindUsd()) > 0.0 
							? cf.format(cpDto.getGrindUsd() + cpflDto.getGrindUsd()) 
							: misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td align=\"left\">Saldo Final</td>");
		out.println("            <td>&nbsp;</td>");
		
		for (i = 0; i < aCostsPlants.size(); i++){
			cpDto 	= (CostsPlants)aCostsPlants.get(i);
			cpflDto	= (CostsPlants)aCostsPlantsFL.get(i);
			
			value = (cpDto.getFinalUsd() + cpflDto.getFinalUsd()) > 0.0 
							? cf.format(cpDto.getFinalUsd() + cpflDto.getFinalUsd()) 
							: misc.NBSP;
			
			out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
		}
		
		out.println("          </tr>");
		out.println("          <tr class=\"columnadatoPar\">");
		out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
		out.println("          </tr>");
	}
	
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");

	//listado de totales
	out.println("          <tr>");
	out.println("            <td class=\"cb_titulocolumna cellBorder\">TOTAL MAÍZ " + (isWaxy ? "WAXY" : "no waxy") + "</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < months; i++){
		mpvDto = (MonthsPeriodsVersions)aMonthPeriodsVersions.get(i);
		
		out.println("            <td align=\"center\" class=\"cb_titulocolumna cellBorder\">" + mpvDto.getShortName() + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">Tons</td>");
	out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getInitialTons() > 0.0 ? tf.format(cpDto.getInitialTons()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}

	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Entradas</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getIncomingTons() > 0.0 ? tf.format(cpDto.getIncomingTons()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Disponible</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getAvailableTons() > 0.0 ? tf.format(cpDto.getAvailableTons()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoNon\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getGrindTons() > 0.0 ? tf.format(cpDto.getGrindTons()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Saldo Final</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getFinalTons() > 0.0 ? tf.format(cpDto.getFinalTons()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">USD Maíz</td>");
	out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getInitialUsd() > 0.0 ? cf.format(cpDto.getInitialUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}

	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Entradas</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getIncomingUsd() > 0.0 ? cf.format(cpDto.getIncomingUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Disponible</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getAvailableUsd() > 0.0 ? cf.format(cpDto.getAvailableUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoNon\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getGrindUsd() > 0.0 ? cf.format(cpDto.getGrindUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Saldo Final</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto = (CostsPlants)aTotals.get(i);
		
		value = cpDto.getFinalUsd() > 0.0 ? cf.format(cpDto.getFinalUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">USD Futuros</td>");
	out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotalsFL.size(); i++){
		cpflDto = (CostsPlants)aTotalsFL.get(i);
		
		value = cpflDto.getInitialUsd() > 0.0 ? cf.format(cpflDto.getInitialUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}

	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Entradas</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotalsFL.size(); i++){
		cpflDto = (CostsPlants)aTotalsFL.get(i);
		
		value = cpflDto.getIncomingUsd() > 0.0 ? cf.format(cpflDto.getIncomingUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Disponible</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotalsFL.size(); i++){
		cpflDto = (CostsPlants)aTotalsFL.get(i);
		
		value = cpflDto.getAvailableUsd() > 0.0 ? cf.format(cpflDto.getAvailableUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoNon\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotalsFL.size(); i++){
		cpflDto = (CostsPlants)aTotalsFL.get(i);
		
		value = cpflDto.getGrindUsd() > 0.0 ? cf.format(cpflDto.getGrindUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Saldo Final</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotalsFL.size(); i++){
		cpflDto = (CostsPlants)aTotalsFL.get(i);
		
		value = cpflDto.getFinalUsd() > 0.0 ? cf.format(cpflDto.getFinalUsd()) : misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"dw_etiqueta_campo cb_titulocolumna\">TOTAL USD</td>");
	out.println("            <td colspan=\"" + (months + 1) + "\">&nbsp;</td>");
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Saldo Inicial</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto 	= (CostsPlants)aTotals.get(i);
		cpflDto	= (CostsPlants)aTotalsFL.get(i);
		
		value = (cpDto.getInitialUsd() + cpflDto.getInitialUsd()) > 0.0 
						? cf.format(cpDto.getInitialUsd() + cpflDto.getInitialUsd()) 
						: misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}

	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Entradas</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto 	= (CostsPlants)aTotals.get(i);
		cpflDto	= (CostsPlants)aTotalsFL.get(i);
		
		value = (cpDto.getIncomingUsd() + cpflDto.getIncomingUsd()) > 0.0 
						? cf.format(cpDto.getIncomingUsd() + cpflDto.getIncomingUsd()) 
						: misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Disponible</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto 	= (CostsPlants)aTotals.get(i);
		cpflDto	= (CostsPlants)aTotalsFL.get(i);
		
		value = (cpDto.getAvailableUsd() + cpflDto.getAvailableUsd()) > 0.0 
						? cf.format(cpDto.getAvailableUsd() + cpflDto.getAvailableUsd()) 
						: misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoNon\">");
	out.println("            <td align=\"left\" class=\"cleanCell\">Salidas</td>");
	out.println("            <td class=\"cleanCell\">&nbsp;</td>");
	
	for (i = 0; i < aCostsPlants.size(); i++){
		cpDto 	= (CostsPlants)aTotals.get(i);
		cpflDto	= (CostsPlants)aTotalsFL.get(i);
		
		value = (cpDto.getGrindUsd() + cpflDto.getGrindUsd()) > 0.0 
						? cf.format(cpDto.getGrindUsd() + cpflDto.getGrindUsd()) 
						: misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td align=\"left\">Saldo Final</td>");
	out.println("            <td>&nbsp;</td>");
	
	for (i = 0; i < aTotals.size(); i++){
		cpDto 	= (CostsPlants)aTotals.get(i);
		cpflDto	= (CostsPlants)aTotalsFL.get(i);
		
		value = (cpDto.getFinalUsd() + cpflDto.getFinalUsd()) > 0.0 
						? cf.format(cpDto.getFinalUsd() + cpflDto.getFinalUsd()) 
						: misc.NBSP;
		
		out.println("            <td align=\"right\" class=\"cellBorder\">" + value + "</td>");
	}
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");
	
	out.println("          </tr>");
	out.println("          <tr class=\"columnadatoPar\">");
	out.println("            <td colspan=\"" + (months + 2) + "\">&nbsp;</td>");
	out.println("          </tr>");

	out.println("        </table>");
	out.println("      </div><br></td>");
	out.println("    </tr>");
	out.println("  </table>");
%>
</form>
<%
out.println("<br> termina proceso");

}catch (Exception e){
	String errorMsg = "";
	errorMsg  = "            <table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">";
	errorMsg += "              <tr>";
	errorMsg += "                <td class=\"errorMsg\">";
	
	errorMsg += "                  Error al procesar el registro";
	errorMsg += "                  <br>Mensaje de error: " + e.getMessage();

	errorMsg += "                </td>";
	errorMsg += "              </tr>";
	errorMsg += "            </table>";
	
	out.println(misc.ERROR + "|" + errorMsg);
} finally {
    try { conn.close(); } catch (Exception e) {}
    try { stmt.close(); } catch (Exception e) {}
    try { rs.close();   } catch (Exception e) {}
}
%>
</body>
</html>